<?php
/**
 * sitechanneltemplate.php
 * Create on 2015-03-26
 * Create by yuebin
 */
namespace haibao\cms\data\site;

class SiteChannelTemplate extends \haibao\cms\data\BaseMysql{

    public function __construct(){
        parent::__construct('\haibao\cms\model\data\site\SiteChannelTemplate');
    }
    
    /**
     * 添加站点频道
     * @param : Object \haibao\cms\model\data\site\SiteChannelTemplate $siteChannelTmpModel
     */
    public function addSiteChannelTemplate($siteChannelTmpModel){
    	$this->beginTranscation();
    	try{
    		$this->add($siteChannelTmpModel);
    	} catch(\Exception $e){
    		$this->rollback();
    		throw $e;
    	}
    	$this->endTranscation();
    }
    
    /**
     * 根据站点ID更新站点频道
     * @param : Object \haibao\cms\model\data\site\SiteChannelTemplate $siteChannelTmpModel
     */
    public function updateSiteChannelTmpById($siteChannelTmpModel){
    	$arrSet =
    	array(
    			\haibao\cms\model\filter\site\SiteChannelTemplate::CONDITION_FIELD_NAME_TEMPLATE_ID => $siteChannelTmpModel->TemplateId,
    			\haibao\cms\model\filter\site\SiteChannelTemplate::CONDITION_FIELD_NAME_AUTO_BUILD => $siteChannelTmpModel->AutoBuild,
    			\haibao\cms\model\filter\site\SiteChannelTemplate::CONDITION_FIELD_NAME_BUILD_CYCLE => $siteChannelTmpModel->BuildCycle,
    			\haibao\cms\model\filter\site\SiteChannelTemplate::CONDITION_FIELD_NAME_FILE_NAME => $siteChannelTmpModel->FileName,
    	);
    	$this->beginTranscation();
    	try{
    		$this->updateById($siteChannelTmpModel->Id, $arrSet);
    	} catch(\Exception $e){
    		$this->rollback();
    		throw $e;
    	}
    	$this->endTranscation();
    }
    
    /**
     * 删除频道模版
     * @param : Int $id
     * @param : Int $status
     */
    public function deleteChannelTmpById($id, $status = \haibao\cms\model\data\site\SiteChannelTemplate::STATUS_ENABLE){
    	$this->updateById($id,
    			array(
    				\haibao\cms\model\filter\site\SiteChannelTemplate::CONDITION_FIELD_NAME_STATUS => $status
    			)
    	);
    }
    
    /**
     * 更新上次修改时间为当前时间
     */
    public function updateLastBuildTime($id){
    	$sql = "update `SiteChannelTemplate` set LastBuildTime=%s where `Id`=%s";
    	$this->query($sql,array(new \DateTime(),$id));
    }
}
